package com.mailbox.mailbox.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mailbox.mailbox.domain.Ip;
import com.mailbox.mailbox.mapper.IpMapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 栗守佳
 */
public class CommenUtils {
    /**
     * 限制一定时间内的次数
     */
    private static final Integer COUNTS=6;

    /**
     * 定义时间
     */
    private static final long TIME=60*60*12;
    /**
     * 获取ip地址
     * @param request
     * @return
     */
    public static  String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static  boolean verifyIp(String ip,IpMapper ipMapper){
        QueryWrapper<Ip> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ip",ip);
        Ip ip1=ipMapper.selectOne(queryWrapper);
        Date date2 = new Date();
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = dateFormat2.format(date2);
        if(ip1==null){
            Ip i=new Ip();
            i.setCounts(1);
            i.setIp(ip);
            i.setIpTime(date2);
            ipMapper.insert(i);
            return true;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String messageTime = dateFormat.format(ip1.getIpTime());
        //两个时间之毫秒差
        try {
            Date NowTime=dateFormat.parse(nowTime);
            Date userPayTime1=dateFormat.parse(messageTime);
            long ms=NowTime.getTime() - userPayTime1.getTime();
            long mm=ms/1000;
            if(mm<TIME && ip1.getCounts()>=COUNTS){
                return false;
            }else if(ip1.getCounts()<COUNTS){
                UpdateWrapper<Ip> updateWrapper=new UpdateWrapper<>();
                updateWrapper.eq("ip",ip);
                Ip ip2=new Ip();
                ip2.setIpTime(date2);
                ipMapper.update(ip2,updateWrapper);
                ipMapper.countsPlus(ip);
                return true;
            }else if (mm>TIME){
                UpdateWrapper<Ip> updateWrapper=new UpdateWrapper<>();
                updateWrapper.eq("ip",ip);
                Ip ip2=new Ip();
                ip2.setIpTime(date2);
                ip2.setCounts(1);
                ipMapper.update(ip2,updateWrapper);
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
